package at.fhj.swd.pse.data;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.logging.Logger;

import at.fhj.swd.pse.domain.CommunityServiceImpl;
import at.fhj.swd.pse.entities.Activity;
import at.fhj.swd.pse.entities.Community;

@Stateless
public class ActivityDAOImpl implements ActivityDAO {
	@PersistenceContext
	private EntityManager m_em;

	private final Logger m_logger = Logger.getLogger(CommunityServiceImpl.class);

	public ActivityDAOImpl(final EntityManager em) {
		this.m_em = em;
	}

	public ActivityDAOImpl() {
	}

	@Override
	public Activity insert(final Activity activity) {
		if (activity != null) {
			return this.m_em.merge(activity);			
		} else {
			throw new NullPointerException("activity");
		}
	}

	@Override
	public Activity get(final int id) {
		return this.m_em.find(Activity.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Activity> getAll() {
		final String hql = "SELECT a FROM Activity a";
		return this.m_em.createQuery(hql).getResultList();
	}

	@Override
	public void remove(final Activity activity) {
		if (activity != null) {
			this.m_em.remove(this.m_em.getReference(Activity.class, activity.getId()));
		} else {
			throw new NullPointerException("activity");
		}

	}

	@Override
	public Activity update(final Activity activity) {
		if (activity != null) {
			this.m_em.merge(activity);
			return activity;
		} else {
			throw new NullPointerException("activity");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Activity> getFriendActivities(final int userId) {
		// TODO Friend filter
		this.m_logger.info("getFriendActivities invoked");

		final String hql = "FROM Activity a where FK_Creator IN (SELECT f.id FROM User u join u.friends f where u.id=:userId) AND Type=:type";

		Query query = this.m_em.createQuery(hql);
		query.setParameter("userId", userId);
		query.setParameter("type", "Friend");
		this.m_logger.info(query.toString());

		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Activity> getOwnActivities(final int userId) {
		this.m_logger.info("getFriendActivities invoked");

		final String hql = "FROM Activity a where FK_Creator=:userId AND Type=:type1 OR Type=:type2";

		Query query = this.m_em.createQuery(hql);
		query.setParameter("userId", userId);
		query.setParameter("type1", "Friend");
		query.setParameter("type2", "Community");
		this.m_logger.info(query.toString());

		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Activity> getGlobalActivities() {
		this.m_logger.info("getGlobalActivities invoked");

		final String hql = "FROM Activity WHERE Type=:type";
		Query query = this.m_em.createQuery(hql);
		query.setParameter("type", "Global");
		this.m_logger.info(query.toString());

		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Activity> getAllCommunityActivities(final int userId) {
		this.m_logger.info("getAllCommunityActivities invoked");

		final String hql = "FROM Activity a where a.m_id IN (SELECT ca.id FROM Community c join c.m_members m join c.m_activities ca where m.id=:userId) AND Type=:type";

		Query query = this.m_em.createQuery(hql);
		query.setParameter("userId", userId);
		query.setParameter("type", "Community");
		this.m_logger.info(query.toString());

		return query.getResultList();

	}
	
	@Override
	public void addToCommunity(int activityId, int communityId) {
		Community community = m_em.find(Community.class, communityId);
		Activity activity = m_em.find(Activity.class, activityId);

		community.addActivity(activity);		
	}
}
